import request from '../utils/request';
import { VehicleData, VehiclePaginatedResponse, VehicleDataRow } from '../types/vehicle';
import { VehicleTeamPaginatedResponse } from '../types/vehicleTeam';

/**
 * Fetch vehicle list data
 * @returns Promise with vehicle data
 */
export const getVehicleList = async (): Promise<VehicleData[]> => {
  try {
    const response: VehiclePaginatedResponse = await request.get('/api/admin/web/vehicle-dtu/page');
    console.log('API Response:', response);
    
    // Check if response is successful
    if (response.code !== '0000') {
      throw new Error(`API Error: ${response.message}`);
    }
    
    // Transform the API response to match the VehicleData interface
    const transformedData: VehicleData[] = response.data.rows.map((row: VehicleDataRow) => ({
      key: row.id.toString(),
      id: row.id,
      deviceId: row.deviceId,
      vehicleNumber: row.vehicleNumber,
      createTime: row.createTime,
      status: row.status,
      capacity: row.capacity,
    }));
    
    console.log('Transformed data:', transformedData);
    return transformedData;
  } catch (error) {
    console.error('Error fetching vehicle data:', error);
    throw error;
  }
};

/**
 * Fetch vehicle team list data with pagination
 * @param params Pagination parameters
 * @returns Promise with paginated vehicle team data
 */
export const getVehicleTeamList = async (params: { 
  current?: number; 
  pageSize?: number;
  [key: string]: any;
}): Promise<VehicleTeamPaginatedResponse> => {
  try {
    const response: VehicleTeamPaginatedResponse = await request.get('/api/admin/web/vehicle-team/page', { params });
    console.log('Vehicle Team API Response:', response);
    
    // Check if response is successful
    if (response.code !== '0000') {
      throw new Error(`API Error: ${response.message}`);
    }
    
    return response;
  } catch (error) {
    console.error('Error fetching vehicle team data:', error);
    throw error;
  }
};